package com.shujia.sql

import org.apache.flink.table.api.{EnvironmentSettings, TableEnvironment}

object Demo12Function {
  def main(args: Array[String]): Unit = {
    val setting: EnvironmentSettings = EnvironmentSettings
      .newInstance()
      .inStreamingMode()
      .build()

    val table: TableEnvironment = TableEnvironment.create(setting)


    /**
      * 在代码中注册自定义函数
      *
      */
    table.createTemporarySystemFunction("split",classOf[Demo13Split])


    table.executeSql(
      """
        |CREATE TABLE words (
        |  lines STRING
        |) WITH (
        |  'connector' = 'kafka',
        |  'topic' = 'lines',
        |  'properties.bootstrap.servers' = 'master:9092',
        |  'properties.group.id' = 'testGroup',
        |  'scan.startup.mode' = 'earliest-offset',
        |  'format' = 'csv',
        |  'csv.field-delimiter'='\t'
        |)
      """.stripMargin)

    /**
      * print sink表
      *
      */
    table.executeSql(
      """
        |CREATE TABLE print_table(
        |lines STRING,
        |s ARRAY<STRING>
        |)
        |WITH ('connector' = 'print')
        |
      """.stripMargin)


    table.executeSql(
      """
        |insert into print_table
        |select lines,split(lines,',') as s from words
        |
      """.stripMargin)


  }

}
